﻿#include "logwindow.h"
#include "ui_logwindow.h"
#include <QPushButton>
#include <QLineEdit>
#include <QMessageBox>
logWindow::logWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::logWindow)
{
    ui->setupUi(this);

    //初始化
    userIndex = 0;
    viewBtn = false;
    ui->viewPwdBtn->setStyleSheet("border-image: url(:/pic/关闭查看.png);");
    ui->pwdEdit->setEchoMode(QLineEdit::Password);
    count = 3;
    //读取用户文件，将用户单个用户信息存储到userStringList， 全部用户信息存储到userList
    QFile file("./user.txt");

    if(!file.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }
    while(!file.atEnd())
    {
        QString str = file.readLine();
        userStringList = str.split("-");
        userList << userStringList;
    }
    file.close();

//    qDebug() << "log:";
//    for (int i = 0; i < userList.size(); i++) {
//        qDebug() << userList[i];
//    }
    //登录及注按钮正常、悬停、按下样式
    ui->logBtn->setStyleSheet("QPushButton#logBtn{border-image: url(:/pic/button2.png);color: rgb(255, 255, 255);}"
                              "QPushButton#logBtn:hover{border-image: url(:/pic/button1.png);color: rgb(255, 255, 255);}"
                              "QPushButton#logBtn:pressed{border-image: url(:/pic/button3.png);color: rgb(255, 255, 255);}");

    ui->regBtn->setStyleSheet("QPushButton#regBtn{border-image: url(:/pic/button2.png);color: rgb(255, 255, 255);}"
                              "QPushButton#regBtn:hover{border-image: url(:/pic/button1.png);color: rgb(255, 255, 255);}"
                              "QPushButton#regBtn:pressed{border-image: url(:/pic/button3.png);color: rgb(255, 255, 255);}");






}

logWindow::~logWindow()
{
    delete ui;
}

void logWindow::openFile()
{
    //读取用户文件，将用户单个用户信息存储到userStringList， 全部用户信息存储到userList
    userStringList.clear();
    userList.clear();
    QFile file("./user.txt");

    if(!file.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }
    while(!file.atEnd())
    {
        QString str = file.readLine();
        userStringList = str.split("-");
        userList << userStringList;
    }
    file.close();
}


/*
 *  登录功能模块：
 *      功能：员工登录、管理员登录、查看密码或关闭密码查看、员工注册
 *      函数：
 *          void logWindow::on_comboBox_currentIndexChanged(int index) 选择登录身份
 *          void logWindow::on_logBtn_clicked() 登录按钮
 *          void logWindow::on_viewPwdBtn_clicked() 密码状态按钮
 *          void logWindow::on_regBtn_clicked() 员工注册按钮
 *
*/

void logWindow::on_comboBox_currentIndexChanged(int index)
{
    //登录身份选择
    userIndex = index;
    if(userIndex == 0)
        ui->userLabel->setText("员工名：");
    if(userIndex == 1)
        ui->userLabel->setText("管理员名：");
}

void logWindow::on_logBtn_clicked()
{


    if(ui->nameEdit->text() == "" || ui->pwdEdit->text() == "")
    {
        QMessageBox::warning(this, "警告", "请填写用户名及密码");
        return;
    }

    for(int i = 0; i < userList.size(); i++)
    {
//        qDebug() << "userList:" << userList[i];
        if(userList[i].at(2).toInt() == userIndex)
        {
            if(userList[i].at(0) == ui->nameEdit->text())
            {
                if(ui->pwdEdit->text() == "")
                {
                    QMessageBox::warning(this, "警告", "请填写密码");
                    return;
                }
                else if(ui->pwdEdit->text() != userList[i].at(1))
                {
                    if(count != 0)
                    {
                        count--;
                        QString warning ="密码错误，你还有%1次机会";
                        warning = warning.arg(count);
                        QMessageBox::warning(this, "警告", warning);
                        return;
                    }
                    else
                    {
                        this->close();
                    }

                }
                else if(userList[i].at(3).at(0) == "1")
                {
                    QMessageBox::warning(this, "警告", "该用户已登录");
                    return;
                }
                else
                {
                    QStringList info;
                    info << userList[i].at(0) << userList[i].at(1) << userList[i].at(2) << "1\r\n";
                    userList.replace(i, info);
                    //修改用户登录状态为1，表示用户已经登录不得重复登录

                    QFile file("./user.txt");

                    if(!file.open(QIODevice::ReadWrite))
                    {
                        qDebug() << "打开文件失败";
                    }

                    file.resize(0);
                    for(int i = 0; i < userList.size(); i++)
                    {
                        QString str = userList[i].at(0) + "-" + userList[i].at(1) + "-" + userList[i].at(2) + "-" + userList[i].at(3);
                        qDebug() << str;
                        file.write(str.toUtf8());
                    }
                    file.close();
                    //QMessageBox::warning(this, "警告", "登陆成功");
                    if(userIndex == 1)
                    {
                        MainWindow* mw = new MainWindow;
                        connect(this, &logWindow::sendUserInfo, mw, &MainWindow::getUserInfo);
                        emit sendUserInfo(userList[i].at(0), userIndex);
                        mw->show();
                        this->close();
                        return;
                    }
                    else if(userIndex == 0)
                    {
                        PuyWindow* pw = new PuyWindow;
                        connect(this, &logWindow::sendUserInfo, pw, &PuyWindow::getUserInfo);
                        emit sendUserInfo(userList[i].at(0), userIndex);
                        pw->show();
                        this->close();
                        return;
                    }

                }
            }
        }
    }
    QMessageBox::warning(this, "警告", "该用户不存在");
    return;
}

void logWindow::on_viewPwdBtn_clicked()
{
    if(!viewBtn)
    {
        viewBtn = !viewBtn;
//        qDebug() << viewBtn;
        ui->viewPwdBtn->setStyleSheet("border-image: url(:/pic/查看密码.png);");
        ui->pwdEdit->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        viewBtn = !viewBtn;
//        qDebug() << viewBtn;
        ui->viewPwdBtn->setStyleSheet("border-image: url(:/pic/关闭查看.png);");
        ui->pwdEdit->setEchoMode(QLineEdit::Password);
    }
}

void logWindow::on_regBtn_clicked()
{

    if(ui->nameEdit->text() == "" || ui->pwdEdit->text() == "")
    {
        QMessageBox::warning(this, "警告", "请填写用户名及密码");
    }
    else if(userIndex == 1)
    {
        QMessageBox::warning(this, "警告", "只能注册员工");
        return;
    }
    else
    {
        QFile file("./user.txt");

        if(!file.open(QIODevice::ReadWrite))
        {
            qDebug() << "打开文件失败";
        }

        QString info = ui->nameEdit->text() + "-" + ui->pwdEdit->text() + "-" + "0" +"-" + "0\r\n";
        file.seek(file.size());
        file.write(info.toUtf8());
        file.close();


        QString name = ui->nameEdit->text();
        //新增用户后购物车界面增加相应信息
        QFile file_addCart("./shoppingcart.json");
        if(!file_addCart.open(QIODevice::ReadWrite))
        {
           qDebug() << "文件打开失败";
        }

        QByteArray array =  file_addCart.readAll();
        QJsonDocument doc = QJsonDocument::fromJson(array);
        QJsonObject usersObj = doc.object();
        QJsonArray resultArray = usersObj.value("users").toArray();
        QJsonObject newUserObj;
        QJsonArray newCartArr;
        newUserObj.insert("name", name);
        newUserObj.insert("cart",newCartArr);
        resultArray.push_back(newUserObj);
        QJsonObject newObj;
        newObj.insert("users", resultArray);
        QJsonDocument n_doc(newObj);
        file_addCart.resize(0);
        file_addCart.write(n_doc.toJson());
        file_addCart.close();

        //新增用户后订单界面增加相应信息
        QFile file_addOrder("./order.json");
        if(!file_addOrder.open(QIODevice::ReadWrite))
        {
           qDebug() << "文件打开失败";
        }

        QByteArray arrayOrder =  file_addOrder.readAll();
        QJsonDocument docOrder = QJsonDocument::fromJson(arrayOrder);
        QJsonObject usersObjOrder = docOrder.object();
        QJsonArray resultArrayOrder = usersObjOrder.value("users").toArray();
        QJsonObject newUserObjOrder;
        QJsonArray newCartArrOrder;
        newUserObjOrder.insert("name", name);
        newUserObjOrder.insert("cart",newCartArrOrder);
        resultArrayOrder.push_back(newUserObjOrder);
        QJsonObject newObjOrder;
        newObjOrder.insert("users", resultArrayOrder);
        QJsonDocument n_docOrder(newObjOrder);
        file_addOrder.resize(0);
        file_addOrder.write(n_docOrder.toJson());
        file_addOrder.close();

        QMessageBox::warning(this,"成功", "注册成功");
        ui->nameEdit->clear();
        ui->pwdEdit->clear();
        openFile();
        return;
    }

}


